/**  
* @Title: SocketOperate.java  
* @Package socket  
* @Description: TODO(用一句话描述该文件做什么)  
* @author Mr.zhao  
* @date 2018年7月24日  
* @version V1.0  
*/
package com.shuodao.cokeps.socket;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shuodao.cokeps.config.YamlConfig;
import com.shuodao.cokeps.controller.BaseController;
import com.shuodao.common.model.ParamMap;
import com.shuodao.common.util.HtmlUtil;
import com.shuodao.common.util.JSON;

/**
 * @author Mr.zhao
 * @date 2018年7月24日
 *
 */
public class SocketOperate extends BaseController implements Runnable{

	private final Logger logger = LoggerFactory.getLogger(SocketOperate.class);

	public void run() {
		byte[] buf = new byte[1024];
		DatagramSocket ds;
		try {
			ds = new DatagramSocket(10001);
			DatagramPacket dp_receive = new DatagramPacket(buf, 1024);
			logger.info("server is on，waiting for client to send data......");
			boolean f = true;
			while(f){
				ds.receive(dp_receive);
				logger.info("server received data from client：");
				String str_receive = new String(dp_receive.getData(),0,dp_receive.getLength()) + 
						" from " + dp_receive.getAddress().getHostAddress() + ":" + dp_receive.getPort();
				logger.info(str_receive);
				String send_url = "http://cokeps.sodo-tech.com/store_scancode.do";
				ParamMap createMap = ParamMap.create("data", str_receive);
				String postData = HtmlUtil.postData(send_url, JSON.toJson(createMap));
				logger.info(postData);
			}
			ds.close();
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
